package com.autumn.annotations.leetcode.two;

/**
 * 项目名称：com.autumn.xa
 * 类 名 称：Demo2
 * 类 描 述：TODO
 * 创建时间：2021/9/15 9:48 下午
 * 创 建 人：yeachrom
 */
public class Demo2 {

    public static String longestPalindrome(String s) {
    // write your code here.
    if (null == s) {
        return "";
    }

    if (s.length() == 0) {
        return s;
    }

    if (s.length() == 1) {
        return s;
    }

    if (s.length() == 2) {
        char[] str = s.toCharArray();
        if (str[0] == str[1]) {
            return s;
        }
        return s.substring(0, 1);
    }
    char[] str = s.toCharArray();
    int start = 0;
    int length = 0;

    for (int i = 0 ; i < s.length() - 1 ; i++) {
        for (int j = i + 1 ; j < s.length() ; j++){
            if (i < j && str[i] == str[j] && check(str, i, j)) {
                int len = j - i;
                if ( length < len ) {
                    length = len ;
                    start = i;
                }
            }
        }
    }
    if (start == 0 && length == 0) {
        return s;
    }
    return s.substring(start, start + length + 1);
}


    public static boolean check(char[] str ,int begin ,int end) {

        if (begin > end || begin == end) {
            return false;
        }

        while (begin < end){
            if (str[begin] == str[end]) {
                begin ++;
                end --;
            } else {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(longestPalindrome("sdfs"));
    }
}
